package dao;

import java.util.Collection;

import model.CreditBean;
import web.dao.Criteria;
import web.dao.GenericDao;
import web.model.UUID;

public class CreditDao extends GenericDao<CreditBean> {

	public CreditDao() {
		super();
	}

	public CreditBean getCreditById(String id) {
		Collection<CreditBean> result = findByProperty("id", id);
		if (result.isEmpty()) {
			return null;
		} else {
			return result.iterator().next();
		}
	}

	public Collection<CreditBean> getCreditByAlbumId(UUID album) {
		Collection<CreditBean> result = findByProperty("album", album);
		return result;
	}

	private CreditBean getCreditByAlbumIdAndRoleAndTracks(UUID album, String index) {
		Criteria[] criteria = new Criteria[2];
		criteria[0] = Criteria.equal("album", album);
		criteria[1] = Criteria.equal("index", index);
		Collection<CreditBean> result = findByCriteria(criteria);
		if (result.isEmpty()) {
			return null;
		} else {
			return result.iterator().next();
		}
	}

	@Override
	public CreditBean find(CreditBean credit) {
		return getCreditByAlbumIdAndRoleAndTracks(credit.getAlbum(), credit.getIndex().toString());
	}
}
